package com.example.fragment.tool;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.fragment.R;
import com.wx.wheelview.widget.WheelView;

import java.util.Arrays;

public class WheelViewUtils {
    Context mContext;
    String mTitle;
    String[] mData;
    AlertDialog ad;
    View mainView;
    WheelView mainWheelView;
    private ImageView mIvSure;

    public WheelViewUtils(Context context, String title, String[] mainData){
        mContext = context;
        mTitle = title;
        mData = mainData;
        initSelecter();
    }

    private void initSelecter(){
        AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
        mainView = View.inflate(mContext, R.layout.select_dialog, null);
        //设置标题
        TextView titleView = mainView.findViewById(R.id.select_title);
        if(mTitle!=null){
        titleView.setText(mTitle);
        }
        mIvSure =mainView.findViewById(R.id.selecter_sure);


        //以下为测试WheelView
        mainWheelView = mainView.findViewById(R.id.wheelview);
        mainWheelView.setSkin(WheelView.Skin.Holo);
        mainWheelView.setWheelSize(5);
        mainWheelView.setLoop(false);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor("#01fcc6");
        style.backgroundColor = Color.parseColor("#011925");
        style.textSize = 20;
        style.selectedTextSize = 25;
        style.textColor = Color.parseColor("#0078b1");
        style.holoBorderColor = Color.parseColor("#00ffff");
        mainWheelView.setWheelAdapter(new SelecterAdapter(mContext));
        mainWheelView.setWheelData(Arrays.asList(mData));
        mainWheelView.setStyle(style);
        ab.setView(mainView);
        ad = ab.create();

        //取消按钮点击事件
        ImageView cancelDia = mainView.findViewById(R.id.selecter_no);
        cancelDia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelDialog();
            }
        });
    }

    public void show(){
        //调节Dialog大小并隐藏导航栏
        Utils.hideButton1(ad);
        ad.show();
        Utils.hideButton2(ad);
        WindowManager.LayoutParams wl = ad.getWindow().getAttributes();
        wl.width = 300;
        wl.height = 350;
//        wl.width = 550;
//        wl.height = 600;
        ad.getWindow().setAttributes(wl);
        ad.getWindow().setBackgroundDrawableResource(R.mipmap.dialog_background);
    }

    //取消WheelView
    public void cancelDialog(){
        ad.dismiss();
    }
    //得到当前位置
    public int getPosition(){

        return mainWheelView.getCurrentPosition();
    }
    //得到确定按钮
    public ImageView getButton(){

        return mainView.findViewById(R.id.selecter_sure);
    }
    public ImageView getmIvSure() {
        return mIvSure;
    }

}
